//
//  Event.m
//  WhoPaidfor
//
//  Created by user on 9/26/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "Event.h"
#import "SBJsonParser.h"
@implementation Event
#define SERVER_URL @"http://nghexaydung.com/whopaid/public/"
static Event *_instance = nil;  // <-- important 
@synthesize _id, _name, _user_id, idEvent, currency;

+(Event *)instance
{ 
	// skip everything
	if(_instance) return _instance; 
    
	// Singleton
	@synchronized([Event class]) 
	{
		if(!_instance)
		{
			_instance = [[self alloc] init];
            
            //	NSLog(@"Creating global instance!"); <-- You should see this once only in your program
		}
        
		return _instance;
	}
    
	return nil;
}

-(NSMutableArray *)getDatasWS:(NSInteger)user_id
{
    NSMutableArray *listData = [[NSMutableArray alloc] init ];
    SBJsonParser *parser = [[SBJsonParser alloc] init];
    NSString *url = [NSString stringWithFormat:@"%@/event/getlistbyuser?user_id=%d",SERVER_URL, user_id];
    NSLog(@"get event list: %@",url);
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
    NSArray *results = [parser objectWithString:json_string error:nil];
    for (NSDictionary *obj in results)
    {
        //NSString *name = [NSString stringWithFormat:@"%@#%@#%@#%@#%@#%d#%d", [obj objectForKey:@"id"], [obj objectForKey:@"name"], [obj objectForKey:@"description"],[obj objectForKey:@"created"], [obj objectForKey:@"totalamount"],[[obj objectForKey:@"active"] intValue], [[obj objectForKey:@"owner_id"] intValue] ];
        //[listData addObject:name];
        EventClass *event = [[EventClass alloc] init];
        event.eventId = [[obj objectForKey:@"id"] intValue];
        event.eventTitle = [obj objectForKey:@"name"];
        event.totalAmount = [[obj objectForKey:@"totalamount"] doubleValue];
        event.currencyId = [[obj objectForKey:@"currency"] intValue];
        event.currency = [obj objectForKey:@"currency_name"];
        event.active = [[obj objectForKey:@"active"] intValue];
        [listData addObject:event];
        
    }
    return listData;
}

-(NSMutableArray *)getOverviewWS:(NSInteger)user_id
{
    NSMutableArray *listData = [[NSMutableArray alloc] init ];
    SBJsonParser *parser = [[SBJsonParser alloc] init];
    NSString *url = [NSString stringWithFormat:@"%@/event/getoverviewbyuser?user_id=%d",SERVER_URL, user_id];
    NSLog(@"get event list: %@",url);
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
    NSArray *results = [parser objectWithString:json_string error:nil];
    for (NSDictionary *obj in results)
    {
        EventClass *event = [[EventClass alloc] init];
        event.eventId = [[obj objectForKey:@"id"] intValue];
        event.eventTitle = [obj objectForKey:@"name"];
        event.totalAmount = [[obj objectForKey:@"totalamount"] doubleValue];
        event.amount = [[obj objectForKey:@"totalofuser"] doubleValue];
        event.currency = [obj objectForKey:@"currency"];
        event.active = [[obj objectForKey:@"active"] intValue];
        [listData addObject:event];
        
    }
    return listData;
}
-(NSMutableArray *)getDetail:(int)event_id
{
    NSMutableArray *listData = [[NSMutableArray alloc] init ];
    SBJsonParser *parser = [[SBJsonParser alloc] init];
    NSString *url = [NSString stringWithFormat:@"%@/event/getdetail?event_id=%d",SERVER_URL, event_id];
    NSLog(@"get event detail: %@",url);
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
    NSArray *results = [parser objectWithString:json_string error:nil];
    for (NSDictionary *obj in results)
    {
        ParticipateClass *event = [[ParticipateClass alloc] init];
        event.userId = [[obj objectForKey:@"user_id"] intValue];
        event.name = [obj objectForKey:@"name"];
        event.price = [[obj objectForKey:@"amount"] doubleValue];
        event.idString = [obj objectForKey:@"facebookid"];
        event.image = [obj objectForKey:@"avatar"];
        event.firstMail = [obj objectForKey:@"mail_address"];
        event.active = [[obj objectForKey:@"active"] intValue];
        [listData addObject:event];
    }
    return listData;
}
-(NSMutableArray *)getListMember:(int)event_id
{
    NSMutableArray *listAll = [[NSMutableArray alloc] init];
    NSMutableArray *listData = [[NSMutableArray alloc] init ];
    SBJsonParser *parser = [[SBJsonParser alloc] init];
    NSString *url = [NSString stringWithFormat:@"%@/event/getinfoandmember?event_id=%d",SERVER_URL, event_id];
    NSLog(@"get event list member: %@",url);
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
    NSDictionary *results = [parser objectWithString:json_string error:nil];
    NSDictionary *valueEvent = [results objectForKey:@"event"];
    NSDictionary *users = [results objectForKey:@"users"];
    EventClass *event = [[EventClass alloc] init];
    event.eventTitle = [valueEvent objectForKey:@"name"];
    event.totalAmount = [[valueEvent objectForKey:@"totalamount"] doubleValue];
    for (NSDictionary *obj in users)
    {
        ParticipateClass *par = [[ParticipateClass alloc] init];
        par.userId = [[obj objectForKey:@"user_id"] intValue];
        par.name = [obj objectForKey:@"name"];
        par.image = [obj objectForKey:@"avatar"];
        par.idString = [obj objectForKey:@"facebookid"];
        par.firstMail = [obj objectForKey:@"mail_address"];
        [listData addObject:par];
    }
    [listAll addObject:event];
    [listAll addObject:listData];
    return listAll;
}
-(int) addEventWS:(NSInteger)user_id eventName:(NSString *)name eventDescription:(NSString *)description Currency:(int)cur
{
    Event *event = [Event instance];
    name = [name stringByReplacingOccurrencesOfString:@" "
                                           withString:@"%20"];
    description = [description stringByReplacingOccurrencesOfString:@" "
                                                         withString:@"%20"];
    NSString *url = [NSString stringWithFormat:@"%@event/add?name=%@&description=%@&user_id=%d&currency=%d",SERVER_URL,name, description,user_id, cur];
    //NSString *contents = [NSString stringWithContentsOfURL:[NSURL URLWithString:url]];
    NSLog(@"link add event %@",url);
    SBJsonParser *parser = [[SBJsonParser alloc] init];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
    NSDictionary *obj = [parser objectWithString:json_string error:nil];
    event._id = [[obj objectForKey:@"event_id"] intValue];
    event._name = [obj objectForKey:@"event_name"];
    return  event._id;
}

-(void)updateEventWS:(int)event_id userId:(int)user_id Name:(NSString *)name Description:(NSString *)description
{
    Event *event = [Event instance];
    name = [name stringByReplacingOccurrencesOfString:@" "
                                           withString:@"%20"];
    description = [description stringByReplacingOccurrencesOfString:@" "
                                                         withString:@"%20"];
    NSString *url = [NSString stringWithFormat:@"%@event/add?id=%d&name=%@&description=%@&user_id=%d",SERVER_URL,event_id,name, description,user_id];
    //NSString *contents = [NSString stringWithContentsOfURL:[NSURL URLWithString:url]];
    NSLog(@"link update event %@",url);
    SBJsonParser *parser = [[SBJsonParser alloc] init];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
    NSDictionary *obj = [parser objectWithString:json_string error:nil];
    event._id = [[obj objectForKey:@"event_id"] intValue];
    event._name = [obj objectForKey:@"event_name"];
    //return  event._id;
}

-(void)deleteEventWS:(NSInteger)event_id userId:(NSInteger)user_id
{
    NSString *url = [NSString stringWithFormat:@"%@event/remove?id=%d&user_id=%d",SERVER_URL,event_id,user_id];
    NSLog(@"link delete event %@",url);
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    //NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
    //NSArray *obj = [parser objectWithString:json_string error:nil];
}


-(void) activeEvent:(NSInteger)user_id eventId:(NSInteger)event_id
{
    NSString *url = [NSString stringWithFormat:@"%@event/activeMember?event_id=%d&user_id=%d",SERVER_URL,event_id, user_id ];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
}

@end
